package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.ReservationOrderDao;
import com.baichang.taoshunda.entity.ReservationOrder;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by ${wushenglu} on 2018/3/13.
 */
@Repository
public class ReservationOrderDaoImpl extends BaseDaoImpl<ReservationOrder> implements ReservationOrderDao {
    /**
     * 用户已确认消费
     *
     * @param id
     * @return
     */
    @Override
    public boolean updateUserStatus(Integer id) {
        String sql = "UPDATE `t_reservation_order` SET `order_state`='1', `user_consume_time`=NOW() WHERE id=?";
        return update(sql, id) > 0;
    }

    @Override
    public List<Map<String, Object>> findReservationOrder(String orderNumber) {
        String sql = "SELECT ro.id, ro.goods_id, ro.order_state, ro.user_consume_time, ro.company_consume_time ," +
                " ro.order_number, ro.goods_name, ro.goods_univalent, g.head_pic AS headPic, ro.goods_spec AS goodsSpec " +
                "FROM `t_reservation_order` ro LEFT JOIN t_goods g ON g.id = ro.goods_id  " +
                "WHERE ro.`order_number` = ?  ";
        return queryForMapList(sql, orderNumber);
    }

    /**
     * @param orderNumber
     * @return
     */
    @Override
    public int getCountByNumber(String orderNumber) {
        String sql = "SELECT COUNT(0) FROM t_reservation_order WHERE order_number=? AND order_state!=2";
        return queryForInt(sql, orderNumber);
    }

    /**
     * 商家已确认消费
     *
     * @param id
     * @return
     */
    @Override
    public boolean updateCompanyStatus(Integer id) {
        String sql = "UPDATE `t_reservation_order` SET `order_state`='2', `company_consume_time`=NOW() WHERE id=?";
        return update(sql, id) > 0;
    }

    @Override
    public int getCountByOrder(String orderNumber) {
        String sql = "SELECT COUNT(0) FROM t_reservation_order WHERE order_number=? ";
        return queryForInt(sql, orderNumber);
    }

    @Override
    public int getCountByOrderAndState0(String orderNumber) {
        String sql = "SELECT COUNT(0) FROM t_reservation_order WHERE order_number=? AND order_state in (0)";
        return queryForInt(sql, orderNumber);
    }

    @Override
    public void updateOrderState(String orderNumber) {
        String sql = "UPDATE `t_reservation_order` SET `order_state`=3 WHERE order_number=? and order_state =0";
        update(sql, orderNumber);
        String sql1 = "UPDATE `t_reservation_order` SET `order_state`=2 WHERE order_number=? and order_state =1";
        update(sql1, orderNumber);
    }

    /**
     * @param orderNumber
     * @param nowPage
     * @param pageSize    @return
     */
    @Override
    public Page queryReservationOrder(String orderNumber, int nowPage, int pageSize) {
        String sql = "SELECT ro.*,tg.head_pic FROM t_reservation_order ro INNER JOIN t_goods tg ON ro.goods_id=tg.id WHERE order_number LIKE ?";
        return pageForMapList(sql, nowPage, pageSize, "%" + orderNumber + "%");
    }

    /**
     * 功能描述:
     * 预约订单总金额
     *
     * @param orderNumber 订单总金额
     * @return:
     * @auther: wsl
     * @date: 2019/2/20 14:26
     */
    @Override
    public double getAllMoneyByOrder(String orderNumber) {
        String sql = " SELECT SUM(goods_univalent) FROM t_reservation_order WHERE order_number=? ";
        return jdbcTemplate.queryForObject(sql, Double.class, orderNumber);
    }

    /**
     * 功能描述:
     * 查询预约退款的退款金额
     * 商家预约没有确认消费的订单金额
     *
     * @param orderNumber 订单编号
     * @return:
     * @auther: wsl
     * @date: 2019/2/20 11:59
     */
    @Override
    public Double getReturnMoney(String orderNumber) {
        String sql = " SELECT SUM(goods_univalent) FROM t_reservation_order WHERE order_number=? AND order_state in (0)";
        return jdbcTemplate.queryForObject(sql, Double.class, orderNumber);
    }
}
